################################################################################
# 
# Koo et al. 2019. Testing legislative shirking in a new setting: 
# the case of lame duck sessions in the Korean National Assembly
# 
# replication.R
# 
# by Bon Sang Koo
#
################################################################################

rm(list=ls())

# setwd("~/Dropbox/2019_05/JJPS_rep")
dat17 <- read.csv("dat_17.csv", header=T)
dat18 <- read.csv("dat_18.csv",header=T,stringsAsFactors=F)
data1 <- read.csv("data_1.csv",header=T)
data2 <- read.csv("data_2.csv", header=T, stringsAsFactors=F)

library(pscl)

################################################################################
# TABLE 2
################################################################################

# model 1: binomial with logit link (zero hurdle model) and 
#     truncated negative binomial model with logit link (count model)
model.1 <- hurdle(rtotal_leg_p ~ depart + motion + seniority + age + type | depart + motion + seniority + age + type, data=dat17, dist = "negbin", zero.dist = "binomial", link = "logit")
summary(model.1)

# model 2: binomial with logit link (zero hurdle model) and 
#     truncated negative binomial model with logit link (count model)
model.2 <- hurdle(rtotal_leg_p ~ depart + ideo_d1 + nideoshirk | depart + motion +  seniority + age + type, data=dat17, dist = "negbin", zero.dist = "binomial", link = "logit")
summary(model.2)

# model 3: binomial with logit link (zero hurdle model) and 
#     truncated negative binomial model with logit link (count model)
model.3 <- hurdle(rtotal_leg_p ~ depart + motion + seniority + age + type + ideo_d1 + nideoshirk | depart + motion + seniority + age + type, data=dat17, dist = "negbin", zero.dist = "binomial", link = "logit")
summary(model.3)

# model 4: binomial with logit link (zero hurdle model) and 
#     truncated negative binomial model with logit link (count model)
model.4 <- hurdle(rtotal_leg_p ~ depart + seniority + motion + age + type | depart + seniority + motion + age + type, data=dat18, dist = "negbin", zero.dist = "binomial", link = "logit")
summary(model.4)

# model 5: binomial with logit link (zero hurdle model) and 
#     truncated negative binomial model with logit link (count model)
model.5 <- hurdle(rtotal_leg_p ~ depart + ideo_d1 + nideoshirk | depart + seniority + motion + age + type, data=dat18, dist = "negbin", zero.dist = "binomial", link = "logit")
summary(model.5)

# model 6: binomial with logit link (zero hurdle model) and 
#     truncated negative binomial model with logit link (count model)
model.6 <- hurdle(rtotal_leg_p ~ depart + motion + seniority + age + type + ideo_d1 + nideoshirk| depart + seniority + motion + age + type, data=dat18, dist = "negbin", zero.dist = "binomial", link = "logit")
summary(model.6)

################################################################################
# TABLE A.1
################################################################################

# model 1: binomial with logit link (zero hurdle model) and 
#     poisson model with logit link (count model)
model.1.a <- hurdle(rtotal_leg_p ~ depart + motion + seniority + age + type |depart + motion + seniority + age + type, data=dat17, dist="poisson", zero.dist = "binomial", link = "logit")
summary(model.1.a)

# model 2: binomial with logit link (zero hurdle model) and 
#     poisson model with logit link (count model)
model.2.a <- hurdle(rtotal_leg_p ~ depart + ideo_d1 + nideoshirk | depart + motion + seniority + age + type, data=dat17, dist="poisson", zero.dist = "binomial", link = "logit")
summary(model.2.a)

# model 3: binomial with logit link (zero hurdle model) and 
#     poisson model with logit link (count model)
model.3.a <- hurdle(rtotal_leg_p ~ depart + motion + seniority + age + type + ideo_d1 + nideoshirk | depart + motion + seniority + age + type, data=dat17, dist="poisson", zero.dist = "binomial", link = "logit")
summary(model.3.a)

# model 4: binomial with logit link (zero hurdle model) and 
#     poisson model with logit link (count model)
model.4.a <- hurdle(rtotal_leg_p ~ depart + motion + seniority + age + type | depart + motion + seniority + age + type, data=dat18, dist="poisson", zero.dist = "binomial", link = "logit")
summary(model.4.a)

# model 5: binomial with logit link (zero hurdle model) and 
#     poisson model with logit link (count model)
model.5.a <- hurdle(rtotal_leg_p ~ depart + ideo_d1 + nideoshirk | depart + motion + seniority + age + type, data=dat18, dist="poisson", zero.dist = "binomial", link = "logit")
summary(model.5.a)

# model 6: binomial with logit link (zero hurdle model) and 
#     poisson model with logit link (count model)
model.6.a <- hurdle(rtotal_leg_p ~ depart + motion + seniority + age + type + ideo_d1 + nideoshirk |depart + motion + seniority + age + type, data=dat18, dist="poisson", zero.dist = "binomial", link = "logit")
summary(model.6.a)

################################################################################
# TABLE 4
################################################################################
# Legislation 27 (DGIST) 
################################################################################
leg27 <- glm(leg27_yeav ~ leaving + seniority + ideo_d1 + dj_chnam + dj_chnam:leaving,
              data = data1, family=binomial(link="logit"))
summary(leg27)

################################################################################
# Legislation 28 (GIST) 
################################################################################
leg28 <- glm(leg28_yeav ~ leaving + seniority + ideo_d1 + dj_chnam + dj_chnam:leaving,
              data = data1, family=binomial(link="logit"))
summary(leg28)

################################################################################
# TABLE 5
################################################################################
# National Assembly Advancement Rule
################################################################################

data2$ideoshirk_leaving <- as.numeric(data2$leaving) * data2$ideo_d1
fit.1 <- glm(leg2_yeav ~ leaving + ideo_d1 + ideoshirk_leaving + seniority + 
                majority, data=data2, family=binomial(link="logit")) 
summary(fit.1)

fit.2 <- glm(leg2_yeav ~ comp_depart + seniority + ideo_d1 + majority +
                     ideoshirk_comp, 
                   data=data2, family=binomial(link="logit")) 
summary(fit.2)

fit.3 <- glm(leg2_yeav ~ notnom_depart + notapp_depart + ideo_d1 + 
                     ideoshirk_notapp + ideoshirk_notnom + seniority + 
                     majority, data=data2, family=binomial(link="logit")) 
summary(fit.3)

data2$app_depart <- factor(data2$app_depart)
data2$ideoshirk_app <- as.numeric(data2$app_depart) * data2$ideo_d1

fit.2a.app <- glm(leg2_yeav ~ app_depart + seniority + ideo_d1 + majority +
                    ideoshirk_app, 
                  data=data2, family=binomial(link="logit")) 
summary(fit.2a.app)

data2$comp_depart <- factor(data2$comp_depart)
data2$ideoshirk_comp <- as.numeric(data2$comp_depart) * data2$ideo_d1

fit.2a.comp <- glm(leg2_yeav ~ comp_depart + seniority + ideo_d1 + majority +
                     ideoshirk_comp, 
                   data=data2, family=binomial(link="logit")) 
summary(fit.2a.comp)

############################################################################ 
# Figure 3
############################################################################
source("interaction_plots_rr.R")

par(mar=c(5,6,4,4), oma=c(0,0,4,0), mfrow=c(1,2))
interaction_plot_continuous(model=fit.2a.app, effect = "app_depart1",
                            moderator = "ideo_d1",
                            interaction = "ideoshirk_app",
                            median = TRUE,
                            breaks=20,
                            num_points = 100,
                            xlabel = "Ideological Distance from the Floor",
                            ylabel = "Marginal effect of Lame Duck Status \n on Yea Vote",
                            title = "Defeated Candidate")
# mtext("National Assembly Rule Amendment", outer = TRUE, cex = 1.5)

interaction_plot_continuous(model=fit.2a.comp, effect = "comp_depart1",
                            moderator = "ideo_d1",
                            interaction = "ideoshirk_comp",
                            median = TRUE,
                            breaks=20,
                            num_points = 100,
                            xlabel = "Ideological Distance from the Floor",
                            ylabel = "Marginal effect of Lame Duck Status \n on Yea Vote",
                            title = "Retiree")
# mtext("National Assembly Rule Amendment", outer = TRUE, cex = 1.5)
